home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6500 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: news.ecp.fr!stefania!dureta8
  2. From: dureta8@cti.ecp.fr (BIG ONE)
  3. Newsgroups: comp.lang.c
  4. Subject: gettimeofday() makes me mad !
  5. Date: 24 Feb 1996 18:20:33 GMT
  6. Organization: Ecole Centrale Paris, France
  7. Distribution: inet
  8. Message-ID: <4gnkth$4on@piston.ecp.fr>
  9. NNTP-Posting-Host: stefania.cti.ecp.fr
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12.     Hello all. If someone could help...
  13.  
  14. COMPILER : gcc, or cc on a Unix system.
  15.  
  16. PROBLEM : I'm trying to make a little program which must repeat an
  17. action every .2 second for example ( less than 1 sec anyway). time()
  18. wouldn't fit, so I searched and found the function gettimeofday() in
  19. <sys/time.h>. It gives the time elapsed since 1970 in seconds and
  20. microseconds. The problem is that when gettimeofday() is called several
  21. times, it finally hangs up ! My program looks like this : 
  22.  
  23.   struct timeval t ;
  24.   long useconds ;
  25.  
  26.   gettimeofday(&t) ;
  27.   printf("%ld\n",(usecondes = t.tv_usec)) ;
  28.  
  29.   for (;;) {
  30.     /* ... */ /*this part works fine */
  31.     gettimeofday(&t) ;
  32.     if (t.tv_usec > usecondes + 200000) {
  33.       printf("%ld\n",(usecondes = t.tv_usec)) ;
  34.       do_smthg() ;
  35.     } 
  36.   }
  37.  
  38. I tried it with time() instead of gettimeofday(), and there was no
  39. problem. Maybe there is another way ? Please help me before I commit
  40. suicide ! 
  41.  
  42. --
  43.      Alexandre Duret
  44. e-mail     : dureta8@cti.ecp.fr
  45.